package com.socialuni.center.web.factory;

import com.socialuni.center.web.dao.UserContactDao;
import com.socialuni.sdk.model.DO.user.UserFollowDetailDO;
import com.socialuni.center.web.model.DO.shell.UserContactDO;
import com.socialuni.center.web.model.DO.user.UserDetailDO;
import com.socialuni.center.web.model.RO.user.CenterUserDetailRO;
import com.socialuni.sdk.repository.UserFollowDetailRepository;
import com.socialuni.center.web.repository.shell.UserContactRepository;
import com.socialuni.center.web.repository.user.UserDetailRepository;
import com.socialuni.center.web.serive.chat.ChatService;
import com.socialuni.center.web.utils.CenterUserUtil;
import com.socialuni.center.web.utils.UnionIdDbUtil;
import com.socialuni.sdk.constant.FollowConst;
import com.socialuni.sdk.factory.user.base.SocialUserDetailROFactory;
import com.socialuni.sdk.model.ChatVO;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.model.RO.SocialUserDetailRO;
import com.socialuni.sdk.model.RO.UserImgRO;
import com.socialuni.sdk.store.SocialUserPhoneStore;
import com.socialuni.sdk.utils.UserUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class CenterUserDetailROFactory {
    public static UserFollowDetailRepository userFollowDetailRepository;
    public static UserDetailRepository userDetailRepository;
    public static SocialUserPhoneStore socialUserPhoneStore;
    public static ChatService chatService;
    public static UserContactRepository userContactRepository;
    public static UserContactDao userContactDao;


    @Resource
    public void setUserFollowDetailRepository(UserFollowDetailRepository userFollowDetailRepository) {
        CenterUserDetailROFactory.userFollowDetailRepository = userFollowDetailRepository;
    }

    @Resource
    public void setUserDetailRepository(UserDetailRepository userDetailRepository) {
        CenterUserDetailROFactory.userDetailRepository = userDetailRepository;
    }

    @Resource
    public void setSocialUserPhoneStore(SocialUserPhoneStore socialUserPhoneStore) {
        CenterUserDetailROFactory.socialUserPhoneStore = socialUserPhoneStore;
    }

    @Resource
    public void setChatService(ChatService chatService) {
        CenterUserDetailROFactory.chatService = chatService;
    }

    @Resource
    public void setUserContactRepository(UserContactRepository userContactRepository) {
        CenterUserDetailROFactory.userContactRepository = userContactRepository;
    }

    @Resource
    public void setUserContactDao(UserContactDao userContactDao) {
        CenterUserDetailROFactory.userContactDao = userContactDao;
    }

    public static CenterUserDetailRO getUserDetailRO(Integer userId) {
        UserDO user = UserUtils.get(userId);
        return CenterUserDetailROFactory.getUserDetailRO(user);
    }

    public static CenterUserDetailRO getUserDetailRO(UserDO user) {
        UserDetailDO userDetailDO = CenterUserUtil.getUserDetail(user.getId());
        return CenterUserDetailROFactory.getUserDetailRO(user, userDetailDO);
    }

    public static CenterUserDetailRO getUserDetailRO(UserDO user, UserDetailDO userDetailDO) {
        //根据用户得到返回详情
        UserDO mineUser = CenterUserUtil.getMineUser();
        return CenterUserDetailROFactory.getUserDetailRO(user, mineUser, userDetailDO);
    }

    public static CenterUserDetailRO getUserDetailRO(UserDO user, UserDO mineUser, UserDetailDO userDetailDO) {
        //根据用户得到返回详情
        SocialUserDetailRO socialUserDetailRO = SocialUserDetailROFactory.getUserDetailRO(user, mineUser);
        return CenterUserDetailROFactory.getUserDetailRO(socialUserDetailRO, mineUser, userDetailDO);
    }

    public static CenterUserDetailRO getUserDetailRO(SocialUserDetailRO socialUserDetailRO, UserDO mineUser, UserDetailDO userDetailDO) {
        UserFollowDetailDO socialUserFollowDetailDO = userFollowDetailRepository.findByUserId(mineUser.getId());
        return CenterUserDetailROFactory.getUserDetailRO(socialUserDetailRO, mineUser, userDetailDO, socialUserFollowDetailDO);
    }

    public static CenterUserDetailRO getUserDetailRO(SocialUserDetailRO socialUserDetailRO, UserDO mineUser, UserDetailDO userDetailDO, UserFollowDetailDO userFollowDetailDO) {
        UserContactDO userContactDO = null;
        if (!socialUserDetailRO.getIsMine()) {
            userContactDO = userContactDao.findByUserIdAndBeUserId(mineUser.getId(), userDetailDO.getId());
        }
        return CenterUserDetailROFactory.getUserDetailRO(socialUserDetailRO, mineUser, userDetailDO, userFollowDetailDO, userContactDO);
    }

    public static CenterUserDetailRO getUserDetailRO(SocialUserDetailRO socialUserDetailRO, UserDO mineUser, UserDetailDO userDetailDO,
                                                     UserFollowDetailDO userFollowDetailDO, UserContactDO userContactDO) {
        //user基础信息
        CenterUserDetailRO userDetailVO = new CenterUserDetailRO(socialUserDetailRO);

        Integer userId = Integer.valueOf(userDetailVO.getId());
        String userUId = UnionIdDbUtil.createUserUid(userId);
        userDetailVO.setId(userUId);

        boolean isMine = userDetailVO.getIsMine();

        List<UserImgRO> imgVOS = userDetailVO.getImgs();

        for (UserImgRO item : imgVOS) {
            item.setId(UnionIdDbUtil.createUserImgUid(item.getId()));
        }

        //设置粉丝数量
        //用户关注粉丝数
        userDetailVO.setFansNum(userFollowDetailDO.getFansNum());
        if (isMine) {
            userDetailVO.setFollowNum(userFollowDetailDO.getFollowNum());
        }


        // user详情相关
        userDetailVO.setReportNum(userDetailDO.getReportNum());
        userDetailVO.setOnlineFlag(userDetailDO.getOnlineFlag());
        userDetailVO.setLastOnlineTime(userDetailDO.getLastOnlineTime());
        userDetailVO.setVipFlag(userDetailDO.getVipFlag());
        userDetailVO.setYearVipFlag(userDetailDO.getYearVipFlag());
        userDetailVO.setFaceRatio(userDetailDO.getFaceRatio());
        userDetailVO.setLoveValue(userDetailDO.getLoveValue());
        userDetailVO.setJusticeValue(userDetailDO.getJusticeValue());
        userDetailVO.setIsSelfAuth(userDetailDO.getIsSelfAuth());
        userDetailVO.setBeSponsorMsgShell(userDetailDO.getBeSponsorMsgShell());
        userDetailVO.setOpenContact(userDetailDO.getOpenContact());


        //联系方式相关
        String contactAccount = userDetailDO.getContactAccount();
        if (isMine) {
            userDetailVO.setContactAccount(contactAccount);
            userDetailVO.setShowUserContact(true);
        } else if (userDetailDO.getOpenContact()) {
            //如果未填写，则显示未填写
            //还有对方是否填写了联系方式
            //是否开启了
            //需要判断用户是否开启了openContact.如果未开启，则showUserContact为false
            //如果为查看别人的详情，则会带着自己的用户信息
            if (userContactDO != null) {
                //这里需要确认用户是否已获取过对方的联系方式
                userDetailVO.setContactAccount(contactAccount);
                userDetailVO.setShowUserContact(true);
            } else {
                userDetailVO.setContactAccount("***" + StringUtils.substring(contactAccount, -3));
                //是否展示联系方式
                userDetailVO.setShowUserContact(false);
            }
        }
        //未登录所有人都显示可关注
        if (mineUser != null && !isMine) {
            //查询出来chatUser，用来判断用户是否购买了。
            //如果被对方关注了，
            ChatVO chat = chatService.seeUserDetailGetOrCreateChat(mineUser, userId);
            userDetailVO.setChat(chat);
        }
        //关注相关
        //他人需要判断
        //应该移动到前端
        if (userDetailVO.getHasFollowed()) {
            if (userDetailVO.getBeFollow()) {
                userDetailVO.setFollowStatus(FollowConst.eachFollow);
            } else {
                userDetailVO.setFollowStatus(FollowConst.followed);
            }
        } else {
            userDetailVO.setFollowStatus(FollowConst.follow);
        }
        return userDetailVO;
    }

    public static CenterUserDetailRO providerLoginGetMineUserDetail(UserDO mineUser, UserDetailDO userDetailDO, UserFollowDetailDO userFollowDetailDO) {
        UserContactDO userContactDO = null;
        //根据用户得到返回详情
        SocialUserDetailRO socialUserDetailRO = SocialUserDetailROFactory.getUserDetailRO(mineUser, mineUser);

        if (!socialUserDetailRO.getIsMine()) {
            userContactDO = userContactDao.findByUserIdAndBeUserId(mineUser.getId(), userDetailDO.getId());
        }
        return CenterUserDetailROFactory.getUserDetailRO(socialUserDetailRO, mineUser, userDetailDO, userFollowDetailDO, userContactDO);
    }

}
